using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using Ent.Web.Helper;
using System.Collections.Generic;
namespace Ent.Web.WebControl
{
    public class Form : Ent.Web.WebControl.UserControl
    {
        XMLHelper _xml = null;
        string _formName = "";
        private TableRow _tr = new TableRow();
        private int _maxcolumn = 1;
        private Table _tab = null;
        FormElement[] _elements = null;
        public int Column
        {
            set { _maxcolumn = value; }
            get { return _maxcolumn; }
        }
        public string Path
        {
            set { _xml = new XMLHelper(value); }
        }
        public string FormName
        {
            set { _formName = value; }
            get { return _formName; }
        }
        public Form(string path, string formName)
        {
            _tab = new Table();
            Controls.Add(_tab);
            _formName = formName;
            _xml = new XMLHelper(path);
            _elements = GetElements();
        }
        public override void Init()
        {
            if (_xml == null)
                throw new Exception("Xml Path has not been provided for Form class");
            if (_formName == "")
                throw new Exception("Form name is Empty");
            _maxcolumn = GetMaxColumn();
            CreateFormElement(Values);
            Flush();
        }
        public override Dictionary<string, object> GetValues()
        {
            Dictionary<string, object> retValue = new Dictionary<string, object>();
            object value;
            foreach (FormElement fe in _elements)
            {
                value = null;
                Control c = FindControl(fe.Name);
                switch (fe.Type)
                {
                    case InputType.Button:
                        value = c != null ? ((Button)c).Text : "";
                        break;
                    case InputType.Checkbox:
                        value = c != null ? ((CheckBox)c).Checked : false;
                        break;
                    case InputType.File:
                        value = c != null ? ((FileUpload)c).FileName : "";
                        break;
                    case InputType.Hidden:
                        value = c != null ? ((HiddenField)c).Value : "";
                        break;
                    case InputType.Image:
                        value = c != null ? ((Image)c).ImageUrl : "";
                        break;
                    case InputType.Password:
                        value = c != null ? ((TextBox)c).Text : "";
                        break;
                    case InputType.Radio:
                        value = c != null ? ((RadioButton)c).Checked : false;
                        break;
                    case InputType.Reset:
                        break;
                    case InputType.Submit:
                        value = c != null ? ((Button)c).Text : "";
                        break;
                    case InputType.Text:
                        value = c != null ? ((TextBox)c).Text : "";
                        break;
                    case InputType.TextArea:
                        value = c != null ? ((TextBox)c).Text : "";
                        break;
                    default:
                    case InputType.None:
                        break;
                }
                retValue.Add(ID + "_" + fe.Name, value);
            }
            return retValue;
        }
        public override void Dispose()
        { }
        public override void SetEvents() { }
        public override void AddJavascript() { }
        public void PopuplateControls() { }
        private void CreateFormElement(Dictionary<string, object> initParams)
        {
            foreach (FormElement fe in _elements)
            {
                string key = fe.Name.Replace(ID + "_", "");
                switch (fe.Type)
                {
                    case InputType.Button:
                        Button btn = new Button();
                        btn.ID = fe.Name;
                        btn.Enabled = !fe.Disabled;
                        btn.Text = initParams.ContainsKey(key) ? initParams[key].ToString() : fe.Value;
                        AddControl(fe.Label, btn);
                        break;
                    case InputType.Checkbox:
                        CheckBox cb = new CheckBox();
                        cb.ID = fe.Name;
                        cb.Checked = initParams.ContainsKey(key) ? Convert.ToBoolean(initParams[key]) : fe.Checked;
                        cb.Enabled = !fe.Disabled;
                        AddControl(fe.Label, cb);
                        break;
                    case InputType.File:
                        FileUpload fu = new FileUpload();
                        fu.ID = fe.Name;
                        fu.Enabled = !fe.Disabled;
                        AddControl(fe.Label, fu);
                        break;
                    case InputType.Hidden:
                        HiddenField hf = new HiddenField();
                        hf.ID = fe.Name;
                        hf.Value = initParams.ContainsKey(key) ? initParams[key].ToString() : fe.Value;
                        AddControl(fe.Label, hf);
                        break;
                    case InputType.Image:
                        Image img = new Image();
                        img.ID = fe.Name;
                        img.ImageUrl = fe.Src;
                        AddControl(fe.Label, img);
                        break;
                    case InputType.Password:
                        TextBox c = new TextBox();
                        c.ID = fe.Name;
                        c.MaxLength = fe.MaxLength <= 0 ? 1 : fe.MaxLength;
                        c.ReadOnly = fe.ReadOnly;
                        c.Enabled = !fe.Disabled;
                        c.Text = initParams.ContainsKey(key) ? initParams[key].ToString() : fe.Value;
                        c.TextMode = TextBoxMode.Password;
                        AddControl(fe.Label, c);
                        break;
                    case InputType.Radio:
                        RadioButton rb = new RadioButton();
                        rb.Checked = initParams.ContainsKey(key) ? Convert.ToBoolean(initParams[key]) : fe.Checked;
                        rb.Enabled = !fe.Disabled;
                        rb.ID = fe.Name;
                        AddControl(fe.Label, rb);
                        break;
                    case InputType.Reset:
                        break;
                    case InputType.Submit:
                        Button btn2 = new Button();
                        btn2.ID = fe.Name;
                        btn2.Enabled = !fe.Disabled;
                        btn2.Text = initParams.ContainsKey(key) ? initParams[key].ToString() : fe.Value;
                        btn2.UseSubmitBehavior = true;
                        AddControl(fe.Label, btn2);
                        break;
                    case InputType.Text:
                        TextBox c2 = new TextBox();
                        c2.ID = fe.Name;
                        c2.MaxLength = fe.MaxLength <= 0 ? 1 : fe.MaxLength;
                        c2.ReadOnly = fe.ReadOnly;
                        c2.Enabled = !fe.Disabled;
                        c2.Text = initParams.ContainsKey(key) ? initParams[key].ToString() : fe.Value;
                        AddControl(fe.Label, c2);
                        break;
                    case InputType.TextArea:
                        TextBox c3 = new TextBox();
                        c3.ID = fe.Name;
                        c3.MaxLength = fe.MaxLength <= 0 ? 1 : fe.MaxLength;
                        c3.ReadOnly = fe.ReadOnly;
                        c3.Enabled = !fe.Disabled;
                        c3.Text = initParams.ContainsKey(key) ? initParams[key].ToString() : fe.Value;
                        c3.TextMode = TextBoxMode.MultiLine;
                        AddControl(fe.Label, c3);
                        break;
                    default:
                    case InputType.None:
                        break;
                }
            }
        }
        private void Flush()
        {
            if (_tr.Cells.Count > 0)
                _tab.Rows.Add(_tr);
        }
        private void AddControl(string label, Control c)
        {
            if (c != null)
            {
                if (_tr.Cells.Count >= _maxcolumn * 2)
                {
                    _tab.Rows.Add(_tr);
                    _tr = new TableRow();
                }
                TableCell tc = new TableCell();
                Label lbl = new Label();
                lbl.Text = label;
                tc.Controls.Add(lbl);
                _tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Controls.Add(c);
                _tr.Cells.Add(tc);
            }
        }
        private int GetMaxColumn()
        {
            string maxCol = "1";
            if(_xml.IfNodeExist("//forms/form[@name='" + _formName + "']"))
                maxCol = _xml.GetAttributeForSingleNode("//forms/form[@name='" + _formName + "']", "colspan");
            return MathFunction.ConvertToInteger(maxCol);
        }
        private FormElement[] GetElements()
        {
            try
            {
                string[] attrList = new string[] { "name", "type", "value", "checked", "maxlength",
                "alt", "disabled", "readonly", "size", "src", "align", "label"};
                DataTable dt = _xml.GetChildNodesAsDataTable("//forms/form[@name='" + _formName + "']", "input", attrList);
                FormElement[] retValue = new FormElement[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    FormElement ele = new FormElement();
                    for (int j = 0; j < attrList.Length; j++)
                    {
                        string value = dt.Rows[i][attrList.GetValue(j).ToString()].ToString();
                        switch (attrList.GetValue(j).ToString().ToLower())
                        { 
                            case "name":  ele.Name = value; break;
                            case "type": ele.Type = FormElement.GetType(value); break;
                            case "value": ele.Value = value; break;
                            case "checked": ele.Checked = MathFunction.ConvertToBoolean(value); break;
                            case "maxlength": ele.MaxLength = MathFunction.ConvertToInteger(value, 50); break;
                            case "alt": ele.Alt = value; break;
                            case "disabled": ele.Disabled = MathFunction.ConvertToBoolean(value); break;
                            case "readonly": ele.ReadOnly = MathFunction.ConvertToBoolean(value); break;
                            case "size": ele.Size = value; break;
                            case "src": ele.Src = value; break;
                            case "align": ele.Align = FormElement.GetAllign(value); break;
                            case "label": ele.Label = value; break;
                        }
                    }
                    retValue.SetValue(ele, i);
                }
                return retValue;
            }
            catch (Exception exc)
            {
                return null;
            }
        }
    }
}
